package com.mdd.maduoduo.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.mdd.maduoduo.R;
import com.mdd.maduoduo.di.component.DaggerUIComponent;
import com.mdd.maduoduo.di.component.UIComponent;
import com.mdd.maduoduo.entity.Response;
import com.mdd.maduoduo.presenter.LoginPresenter;
import com.mdd.maduoduo.utils.LoadingUtils;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.utils.Utils;
import com.mdd.maduoduo.view.interfaces.AddMsgView;
import com.youth.banner.util.LogUtils;

import javax.inject.Inject;

public class AddMsgActivity extends BaseActivity implements AddMsgView {

    @Inject
    public LoginPresenter mPresenter;

    private ImageView ivBack;

    private RelativeLayout rlAdd;

    private EditText etCode;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_add_msg;
    }

    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    protected void initializeInjector() {
        super.initializeInjector();
        mPresenter = new LoginPresenter();
        UIComponent uiComponent = DaggerUIComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule()).build();
        uiComponent.inject(this);
        mPresenter.setAddMsgView(this);
    }

    @Override
    protected void initView() {
        super.initView();
        ivBack = findViewById(R.id.ivBack);
        rlAdd = findViewById(R.id.rlAdd);
        etCode = findViewById(R.id.etCode);
    }

    @Override
    protected void onClick() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        rlAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String smsContent = etCode.getText().toString();
                if (smsContent.isEmpty()){
                    ToastUtils.showLongToast(AddMsgActivity.this,"请输入取件码短信");
                    return;
                }
                if (Utils.isFastClick()){
                    LoadingUtils.showLoading(AddMsgActivity.this);
                    mPresenter.addMsg(AddMsgActivity.this,smsContent);
                }
            }
        });

    }

    @Override
    public void renderSuccess(Response response) {
        LoadingUtils.hideLoading();
        if (response.code.equals("200")) {
            finish();
        }else {
            ToastUtils.showLongToast(AddMsgActivity.this,response.msg);
        }

    }
}
